#include "ro635_server.hpp"
#include "motion_control.hpp"

namespace ro635_server
{
    const char *ssid = "ESP32_AP";
    const char *password = "12345678";

    WebServer web_server(80);

    /**
     * @brief 根页面
     * @note 通过 esp32的ip(192.168.4.1) 可以访问到这个页面
     */
    void handleRootPage()
    {
        String html = "<head><meta charset='utf-8'></head>\
<script>var xhttp = new XMLHttpRequest();\
    function turnPage(page){\
        xhttp.open('GET', '/turn_page?page='+page, true);\
        xhttp.send();\
    }\
</script>\
<h1>Hello from ESP32!</h1>\
<center>\
    <button style='width:300px; height:100px; font-size: 30px;' onclick='turnPage(\"control\")'>控制页面</button></br>\
</center>\
";
        web_server.send(200, "text/html", html);
    }

    /**
     * @brief 控制页面
     * @note 通过 (192.168.4.1/control) 可以访问到这个页面
     */
    void handleControlPage()
    {
        String html = "<head><meta charset='utf-8'></head>\
<script>var xhttp = new XMLHttpRequest();\
    function controlTablePet(code){\
        xhttp.open('GET', '/control_table_pet?ctrl_code='+code, true);\
        xhttp.send();\
    }\
</script>\
<h1>Hello from ESP32!</h1>\
<center>\
    this page is used to control the servo motor.</br>\
    这个页面将用于兵蚁ro635的控制</br>\
    </br>\
    <button style='width:200px; height:200px; margin: 10px; font-size: 100px;' onclick='controlTablePet(\"forward\")'>▲</button></br>\
    <button style='width:200px; height:200px; margin: 10px; font-size: 100px;' onclick='controlTablePet(\"left\")'>◀</button>\
    <button style='width:200px; height:200px; margin: 10px; font-size: 100px;' onclick='controlTablePet(\"stop\")'>◆</button>\
    <button style='width:200px; height:200px; margin: 10px; font-size: 100px;' onclick='controlTablePet(\"right\")'>▶</button></br>\
    <button style='width:200px; height:200px; margin: 10px; font-size: 100px;' onclick='controlTablePet(\"backward\")'>▼</button>\
</center>\
";
        web_server.send(200, "text/html", html);
    }

    /**
     * @brief 用于处理桌宠的控制信息
     * @note 控制页面的按钮会访问这个接口
     */
    void handleControlTablePet()
    {
        String ctrl_code = web_server.arg("ctrl_code");
        if (ctrl_code == "forward")
        {
            Serial.println("table pet will move forward");
            moveForward();
        }
        else if (ctrl_code == "left")
        {
            Serial.println("table pet will turn left");
            turnLeft();
        }
        else if (ctrl_code == "right")
        {
            Serial.println("table pet will turn right");
            turnRight();
        }
        else if (ctrl_code == "backward")
        {
            Serial.println("table pet will move backward");
            moveBackward();
        }
        else
        {
            Serial.println("table pet will stop");
            stop();
        }
    }

    /**
     * @brief 用于跳转页面
     */
    void handleTurnPage()
    {
        String page = web_server.arg("page");
        if (page == "control")
        {
            handleControlPage();
        }
    }

    void creatWebServer()
    {
        WiFi.softAP(ssid, password);
        Serial.println("热点已创建");
        Serial.print("IP address: ");
        Serial.println(WiFi.softAPIP());

        web_server.on("/", handleRootPage);
        web_server.on("/control", handleControlPage);
        web_server.on("/control_table_pet", handleControlTablePet);
        web_server.on("/turn_page", handleTurnPage);
        web_server.begin();
    }
} // namespace ro635_server